
using System.Windows.Forms;
using DevExpress.XtraBars;
using DevExpress.XtraTab;

namespace MobileCommissionManagement
{
    public partial class Main : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        private Controller _controller;
        private VodafoneController _vodafoneController;

        #region Properties

        public Controller Controller
        {
            set
            {
                _controller = value;
                zoneList.Controller = _controller;
                entityTypeList.Controller = _controller;
                entityList.Controller = _controller;
                vodafoneBusinessList.GlobalController = _controller;
            }
        }

        public VodafoneController VodafoneController
        {
            set
            {
                _vodafoneController = value;
                vodafoneDealerConfigurationList.VodafoneController = _vodafoneController;
                vodafoneDealerList.VodafoneController = _vodafoneController;
                vodafoneClientList.VodafoneController = _vodafoneController;
                vodafoneBusinessList.VodafoneController = _vodafoneController;
                vodafoneTariffList.VodafoneController = _vodafoneController;
                vodafoneBusinessItemTypeList.VodafoneController = _vodafoneController;
            }
        }

        #endregion

        #region Base Events

        private Control GetControl()
        {
            if (xtraTabControl.SelectedTabPage == null)
                return null;
            if (xtraTabControl.SelectedTabPage.Controls.Count == 0)
                return null;
            var control = xtraTabControl.SelectedTabPage.Controls[0];
            return control as IListAction == null ? null : control;
        }

        void removeBarButtonItem_ItemClick(object sender, ItemClickEventArgs e)
        {
            var control = GetControl();
            if (control == null)
                return;
            ((IListAction)control).Remove();                            
        }

        void changeSituationBarButtonItem_ItemClick(object sender, ItemClickEventArgs e)
        {
            var control = GetControl();
            if (control == null)
                return;
            ((IListAction)control).ChangeSituation();
        }

        void editBarButtonItem_ItemClick(object sender, ItemClickEventArgs e)
        {
            var control = GetControl();
            if (control == null)
                return;
            ((IListAction)control).Edit();
        }

        void addBarButtonItem_ItemClick(object sender, ItemClickEventArgs e)
        {
            var control = GetControl();
            if (control == null)
                return;
            ((IListAction)control).Add();
        }

        private void exitBarButtonItem_ItemClick(object sender, ItemClickEventArgs e)
        {
            Close();
        }        

        #endregion

        #region Maintenance Events

        void zoneBarButtonItem_ItemClick(object sender, ItemClickEventArgs e)
        {
            zoneXtraTabPage.PageVisible = true;
            xtraTabControl.SelectedTabPage = zoneXtraTabPage;
        }

        void listEntityBarButtonItem_ItemClick(object sender, ItemClickEventArgs e)
        {
            entityXtraTabPage.PageVisible = true;
            xtraTabControl.SelectedTabPage = entityXtraTabPage;
        }

        void listEntityTypesBarButtonItem_ItemClick(object sender, ItemClickEventArgs e)
        {
            entityTypeXtraTabPage.PageVisible = true;
            xtraTabControl.SelectedTabPage = entityTypeXtraTabPage;
        }

        #endregion Maintenance Events

        #region VodafoneCorporate Events

        void vodafoneDealerSetupBarButtonItem_ItemClick(object sender, ItemClickEventArgs e)
        {
            vodafoneDealerConfigXtraTabPage.PageVisible = true;
            xtraTabControl.SelectedTabPage = vodafoneDealerConfigXtraTabPage;
        }

        void vodafoneDealerFilterBarButtonItem_ItemClick(object sender, ItemClickEventArgs e)
        {
            vodafoneDealerXtraTabPage.PageVisible = true;
            xtraTabControl.SelectedTabPage = vodafoneDealerXtraTabPage;
            _vodafoneController.OnShowingDealerFilter();
        }

        void vodafoneDealerImportBarButtonItem_ItemClick(object sender, ItemClickEventArgs e)
        {
            vodafoneDealerXtraTabPage.PageVisible = true;
            xtraTabControl.SelectedTabPage = vodafoneDealerXtraTabPage;
            _vodafoneController.OnShowingDealerImport();
        }

        void vodafoneClientsBarButtonItem_ItemClick(object sender, ItemClickEventArgs e)
        {
            vodafoneClientXtraTabPage.PageVisible = !vodafoneClientXtraTabPage.PageVisible;
            xtraTabControl.SelectedTabPage = vodafoneClientXtraTabPage.PageVisible ? vodafoneClientXtraTabPage : null;            
        }

        void vodafoneBusinessBarButtonItem_ItemClick(object sender, ItemClickEventArgs e)
        {
            vodafoneBusinessXtraTabPage.PageVisible = !vodafoneBusinessXtraTabPage.PageVisible;
            xtraTabControl.SelectedTabPage = vodafoneBusinessXtraTabPage.PageVisible ? vodafoneBusinessXtraTabPage : null;            
        }

        void vodafoneTariffBarButtonItem_ItemClick(object sender, ItemClickEventArgs e)
        {            
            vodafoneTariffXtraTabPage.PageVisible = !vodafoneTariffXtraTabPage.PageVisible;
            xtraTabControl.SelectedTabPage = vodafoneTariffXtraTabPage.PageVisible ? vodafoneTariffXtraTabPage : null;            
        }

        void vodafoneBusinessItemTypeBarButtonItem_ItemClick(object sender, ItemClickEventArgs e)
        {
            vodafoneBusinessItemTypeXtraTabPage.PageVisible = !vodafoneBusinessItemTypeXtraTabPage.PageVisible;
            xtraTabControl.SelectedTabPage = vodafoneBusinessItemTypeXtraTabPage.PageVisible ? vodafoneBusinessItemTypeXtraTabPage : null;            
        }

        #endregion VodafoneCorporate Events

        private void SetNotVisibleAllTabPages()
        {
            foreach (XtraTabPage tabPage in xtraTabControl.TabPages)
                tabPage.PageVisible = false;
        }

        public Main()
        {
            InitializeComponent();
            xtraTabControl.SelectedPageChanged += xtraTabControl_SelectedPageChanged;
            SetNotVisibleAllTabPages();
            zoneBarButtonItem.ItemClick += zoneBarButtonItem_ItemClick;
            listEntityTypesBarButtonItem.ItemClick += listEntityTypesBarButtonItem_ItemClick;
            listEntityBarButtonItem.ItemClick += listEntityBarButtonItem_ItemClick;
            // Base
            addBarButtonItem.ItemClick += addBarButtonItem_ItemClick;
            editBarButtonItem.ItemClick += editBarButtonItem_ItemClick;
            changeSituationBarButtonItem.ItemClick += changeSituationBarButtonItem_ItemClick;
            removeBarButtonItem.ItemClick += removeBarButtonItem_ItemClick;
            // Vodafone
            vodafoneDealerSetupBarButtonItem.ItemClick += vodafoneDealerSetupBarButtonItem_ItemClick;
            vodafoneDealerImportBarButtonItem.ItemClick += vodafoneDealerImportBarButtonItem_ItemClick;
            vodafoneDealerFilterBarButtonItem.ItemClick += vodafoneDealerFilterBarButtonItem_ItemClick;
            vodafoneTariffBarButtonItem.ItemClick += vodafoneTariffBarButtonItem_ItemClick;
            vodafoneBusinessItemTypeBarButtonItem.ItemClick += vodafoneBusinessItemTypeBarButtonItem_ItemClick;
            vodafoneBusinessBarButtonItem.ItemClick += vodafoneBusinessBarButtonItem_ItemClick;
            vodafoneClientsBarButtonItem.ItemClick += vodafoneClientsBarButtonItem_ItemClick;
            // TMN
            tmnDealerRibbonPageGroup.Visible = false;
            // Optimus
            optimusDealerRibbonPageGroup.Visible = false;            
        }

        void xtraTabControl_SelectedPageChanged(object sender, TabPageChangedEventArgs e)
        {
            var control = GetControl();
            addBarButtonItem.Visibility = control == null ? BarItemVisibility.Never :
                ((IListAction)control).IsAddVisible ? BarItemVisibility.Always : BarItemVisibility.Never;
            addBarButtonItem.Enabled = control == null ? false : ((IListAction)control).IsAddEnable;
            editBarButtonItem.Visibility = control == null ? BarItemVisibility.Never :
                ((IListAction)control).IsEditVisible ? BarItemVisibility.Always : BarItemVisibility.Never;
            editBarButtonItem.Enabled = control == null ? false : ((IListAction)control).IsEditEnable;
            changeSituationBarButtonItem.Visibility = control == null ? BarItemVisibility.Never :
                ((IListAction)control).IsChangeSituationVisible ? BarItemVisibility.Always : BarItemVisibility.Never;
            changeSituationBarButtonItem.Enabled = control == null ? false : ((IListAction)control).IsChangeSituationEnable;
            removeBarButtonItem.Visibility = control == null ? BarItemVisibility.Never :
                ((IListAction)control).IsRemoveVisible ? BarItemVisibility.Always : BarItemVisibility.Never;
            removeBarButtonItem.Enabled = control == null ? false : ((IListAction)control).IsRemoveEnable;
        }
        
    }
}